package com.hk.service.order.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Created by lipei on 2017/9/25.
 */
@Service
public class FixedAssetsOrderImpl implements Callable<Map<String, Object>> {

    private  Long userId;

    private Logger logger = LoggerFactory.getLogger(FixedAssetsOrderImpl.class);


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public FixedAssetsOrderImpl() {
    }


    @Override
    public Map<String, Object>  call() throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

        Map<String, Object> fixedAssetsObject=new HashMap<>();
        fixedAssetsObject.put("fixedAssetsCounts", 0);
        fixedAssetsObject.put("fixedAssetsSuccessAmts", BigDecimal.valueOf(0));
        fixedAssetsObject.put("fixedAssetsCurrentDividends", BigDecimal.valueOf(0));
        fixedAssetsObject.put("fixedAssetsHistoryDividends", BigDecimal.valueOf(0));


        try {

            Map<String, Object> fixedAssetsMap=getFixedAssets( this.userId);
            int fixedAssetsCounts=(int)fixedAssetsMap.get("allCounts");
            BigDecimal fixedAssetsSuccessAmts=(BigDecimal)fixedAssetsMap.get("successAmts");
            BigDecimal fixedAssetsCurrentDividends=(BigDecimal)fixedAssetsMap.get("currentDividends");
            BigDecimal fixedAssetsHistoryDividends=(BigDecimal)fixedAssetsMap.get("historyDividends");
            fixedAssetsObject.put("fixedAssetsCounts", fixedAssetsCounts);
            fixedAssetsObject.put("fixedAssetsSuccessAmts", fixedAssetsSuccessAmts);
            fixedAssetsObject.put("fixedAssetsCurrentDividends", fixedAssetsCurrentDividends);
            fixedAssetsObject.put("fixedAssetsHistoryDividends", fixedAssetsHistoryDividends);

        } catch (Exception e) {
            logger.info("查询总资产首页allData异常!userId={}", userId);
            logger.error("<-----------------------------------error----------------------------------->", e);
        }

        return fixedAssetsObject;
    }


    /* 固定资产*/
    private Map<String, Object> getFixedAssets(Long  userId) {
        Map<String, Object> resultMap=new HashMap();
        String countSql = "select count(1) from t_order_info where payStatus=2 and userId = ?";
        String moneySql = "select (select sum(successAmount) from t_order_info where payStatus=2 and userId = ? and benefitType <> 2 ) as payBackAmts,(select sum(dividend) from t_order_info where payStatus = 2 and userId = ?)  as currentDividends,(select sum(dividend) from t_order_info where payStatus in (2,4) and userId = ?)  as historyDividends";
        Map<String, Object> moneyMap = this.jdbcTemplate.queryForMap(moneySql, new Object[] {userId, userId, userId});


        int allCounts = this.jdbcTemplate.queryForObject(countSql, new Object[] {userId}, Integer.class);
        BigDecimal  payBackAmts=BigDecimal.valueOf(0);
        BigDecimal  historyDividends=BigDecimal.valueOf(0);
        BigDecimal  currentDividends=BigDecimal.valueOf(0);
        if(moneyMap.get("payBackAmts")!=null){
            payBackAmts=((BigDecimal)moneyMap.get("payBackAmts")).setScale(4,BigDecimal.ROUND_DOWN);
        }
        if(moneyMap.get("historyDividends")!=null){
            historyDividends=((BigDecimal)moneyMap.get("historyDividends")).setScale(4,BigDecimal.ROUND_DOWN);
        }
        if(moneyMap.get("currentDividends")!=null){
            currentDividends=((BigDecimal)moneyMap.get("currentDividends")).setScale(4,BigDecimal.ROUND_DOWN);
        }
        resultMap.put("allCounts", allCounts);
        resultMap.put("currentDividends", currentDividends);
        resultMap.put("successAmts", payBackAmts);
        resultMap.put("historyDividends", historyDividends);
        return resultMap;
    }
}
